package threadpoolnio;

import threadpoolnio.threadpool.NamedThreadFactory;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;

/**
 * @author ：xx
 * @date ：Created in 2020/2/27 18:50
 * @description：
 * @modified By：
 */
public class TCPServer_Thread extends Thread {
    private  ServerSocket ss=null;
    public static int clientCount=0;
    private    ExecutorService threadPool=null;
    private  boolean flag=true;
    public TCPServer_Thread(){
        try {
            ss = new ServerSocket(9999);
            int cpuNums=Runtime.getRuntime().availableProcessors();
            //threadPool= Executors.newFixedThreadPool(2*cpuNums);
            int poolSize=2*cpuNums;
//            poolSize=1;
            threadPool=new ThreadPoolExecutor(poolSize, poolSize, 0, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(1),new NamedThreadFactory("线程"),new RejectedExecutionHandler() {
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    System.out.println("当然任务被拒绝了："+r.toString());
                }
            }){
                @Override
                protected void beforeExecute(Thread t, Runnable r) {
                    super.beforeExecute(t, r);

                    System.out.println("当前的任务数"+this.getTaskCount()+"已完成的任务数："+this.getCompletedTaskCount()+"等待执行……"+r.toString());
                }

                @Override
                protected void afterExecute(Runnable r, Throwable t) {
                    super.afterExecute(r, t);
                    System.out.println("执行完毕……"+r.toString());
                }

                @Override
                protected void terminated() {
                    super.terminated();
                    System.out.println("线程池结束");
                }
            };
        } catch (IOException e) {
            System.out.println("绑定端口失败："+e.getMessage());
        }
    }
    public static void main(String[] args) throws IOException {
        TCPServer_Thread tcpServer_thread=new TCPServer_Thread();
        tcpServer_thread.start();

    }

    @Override
    public void run() {
        try {
            while (flag) {
                Socket s = ss.accept();
                clientCount++;
                SocketThread socketThread = new SocketThread(s, clientCount);
                //socketThread.start();//交给线程池来做就可以了
                threadPool.execute(socketThread);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(ss!=null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public void stopServer(){
        if(ss!=null) {
            this.flag=false;
            try {
               Socket s=new Socket("localhost",9999);
                if(threadPool!=null)
                    threadPool.shutdown();
//               ss.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
